package com.example.beautyimage;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;

/**
 * 美图工具类
 * Created by 郑甜饼 on 2018/7/7.
 */

public class ImageHelper {
    public static Bitmap handleImageEffect(Bitmap bitmap,float hue,float saturation,float lum){
        Bitmap bitmap1 = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),Bitmap.Config.ARGB_8888);
        //弄一个画布,所有操作都在画布上执行
        Canvas canvas = new Canvas(bitmap1);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        //色度
        ColorMatrix hueMatrix = new ColorMatrix();
        hueMatrix.setRotate(0,hue);
        hueMatrix.setRotate(1,hue);
        hueMatrix.setRotate(2,hue);
        //饱和度
        ColorMatrix saturationMatrix = new ColorMatrix();
        saturationMatrix.setSaturation(saturation);
        //亮度
        ColorMatrix lumMatrix = new ColorMatrix();
        lumMatrix.setScale(lum,lum,lum,1);
        //图片信息
        ColorMatrix imageMatrix = new ColorMatrix();
        imageMatrix.postConcat(hueMatrix);
        imageMatrix.postConcat(saturationMatrix);
        imageMatrix.postConcat(lumMatrix);
        //把图片在画布上展现出来
        paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
        canvas.drawBitmap(bitmap,0,0,paint);
        return bitmap1;
    }
    //底片效果
    public static Bitmap handleImageNegative(Bitmap bitmap){
        int mWidth = bitmap.getWidth();
        int mHeight = bitmap.getHeight();
        int mColor,r,g,b,a;
        Bitmap bitmap1 = Bitmap.createBitmap(mWidth,mHeight,Bitmap.Config.ARGB_8888);
        //保存像素点数组到图像
        int[] oldPx = new int[mWidth*mHeight];//原来的像素组
        int[] newPx = new int[mWidth*mHeight];//新的像素组

        bitmap.getPixels(oldPx,0,mWidth,0,0,mWidth,mHeight);
        for (int i = 0;i<mWidth*mHeight;i++){
            mColor = oldPx[i];
            r = Color.red(mColor);
            g = Color.green(mColor);
            b = Color.blue(mColor);
            a = Color.alpha(mColor);

            r = 255-r;
            g = 255-g;
            b = 255-b;

            if (r>255){
                r=255;
            }else if (r<0){
                r = 0;
            }
            if (g>255){
                g=255;
            }else if (g<0){
                g = 0;
            }
            if (b>255){
                b=255;
            }else if (b<0){
                b = 0;
            }
            newPx[i] = Color.argb(a,r,g,b);
        }
        bitmap1.setPixels(newPx,0,mWidth,0,0,mWidth,mHeight);
        return bitmap1;
    }
    //怀旧效果
    public static Bitmap handleImageReminiscence(Bitmap bitmap){
        int mWidth = bitmap.getWidth();
        int mHeight = bitmap.getHeight();
        int mColor,r,g,b,a;
        Bitmap bitmap1 = Bitmap.createBitmap(mWidth,mHeight,Bitmap.Config.ARGB_8888);
        //保存像素点数组到图像
        int[] oldPx = new int[mWidth*mHeight];//原来的像素组
        int[] newPx = new int[mWidth*mHeight];//新的像素组

        bitmap.getPixels(oldPx,0,mWidth,0,0,mWidth,mHeight);
        for (int i = 0;i<mWidth*mHeight;i++){
            mColor = oldPx[i];
            r = Color.red(mColor);
            g = Color.green(mColor);
            b = Color.blue(mColor);
            a = Color.alpha(mColor);

            r = (int) (0.393 * r + 0.769 * g + 0.189 * b);
            g = (int) (0.349 * r + 0.686 * g + 0.168 * b);
            b = (int) (0.272 * r + 0.534 * g + 0.131 * b);

            if (r>255){
                r=255;
            }else if (r<0){
                r = 0;
            }
            if (g>255){
                g=255;
            }else if (g<0){
                g = 0;
            }
            if (b>255){
                b=255;
            }else if (b<0){
                b = 0;
            }
            newPx[i] = Color.argb(a,r,g,b);
        }
        bitmap1.setPixels(newPx,0,mWidth,0,0,mWidth,mHeight);
        return bitmap1;
    }
    //浮雕效果
    public static Bitmap handleImageRelief(Bitmap bitmap){
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        int color = 0;
        int colorBefore = 0;

        int r, g, b, a;

        int r1, b1, g1;

        int oldPx[] = new int[width * height];
        int newPx[] = new int[width * height];

        Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        bitmap.getPixels(oldPx, 0, width, 0, 0, width, height);

        //注意要从1开始！！！
        for (int i = 1; i < width * height; i++) {

            colorBefore = oldPx[i - 1];
            a = Color.alpha(colorBefore);
            r = Color.red(colorBefore);
            g = Color.green(colorBefore);
            b = Color.blue(colorBefore);

            color = oldPx[i];

            r1 = Color.red(color);
            g1 = Color.green(color);
            b1 = Color.blue(color);

            r = r - r1 + 127;
            g = g - g1 + 127;
            b = b - b1 + 127;

            if (r > 255) {
                r = 255;
            }

            if (g > 255) {
                g = 255;
            }

            if (b > 255) {
                b = 255;
            }

            newPx[i] = Color.argb(a, r, g, b);

        }

        bmp.setPixels(newPx, 0, width, 0, 0, width, height);

        return bmp;
    }
    //感性效果
    public static Bitmap handleImageSensibility(Bitmap bitmap){
        int mWidth = bitmap.getWidth();
        int mHeight = bitmap.getHeight();
        int mColor,r,g,b,a;
        Bitmap bitmap1 = Bitmap.createBitmap(mWidth,mHeight,Bitmap.Config.ARGB_8888);
        //保存像素点数组到图像
        int[] oldPx = new int[mWidth*mHeight];//原来的像素组
        int[] newPx = new int[mWidth*mHeight];//新的像素组

        bitmap.getPixels(oldPx,0,mWidth,0,0,mWidth,mHeight);
        for (int i = 0;i<mWidth*mHeight;i++){
            mColor = oldPx[i];
            r = Color.red(mColor);
            g = Color.green(mColor);
            b = Color.blue(mColor);
            a = Color.alpha(mColor);

            //R 分量
            r = r * 128 / (g + b + 1);
            //G 分量
            g = g * 128 / (b + r + 1);
            //B 分量
            b = b * 128 / (r + g + 1);


            if (r>255){
                r=255;
            }else if (r<0){
                r = 0;
            }
            if (g>255){
                g=255;
            }else if (g<0){
                g = 0;
            }
            if (b>255){
                b=255;
            }else if (b<0){
                b = 0;
            }
            newPx[i] = Color.argb(a,r,g,b);
        }
        bitmap1.setPixels(newPx,0,mWidth,0,0,mWidth,mHeight);
        return bitmap1;
    }
    public static Bitmap handleImageNight(Bitmap bitmap){

        int w = bitmap.getWidth();
        int h = bitmap.getHeight();

        Bitmap resultBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
        int color = 0;
        int r,g,b,r1,g1,b1,r2,g2,b2;

        int[] oldPx = new int[w * h];
        int[] newPx = new int[w * h];

        bitmap.getPixels(oldPx, 0, w, 0, 0, w, h);
        for(int y = 0; y < h - 1; y++){
            for(int x = 0; x < w - 1; x++){
                color = oldPx[x+y*w];

                r = (color >> 16) & 0xFF;
                g = (color >> 8) & 0xFF;
                b = (color >> 0) & 0xFF;

                int newcolor = oldPx[x+1+y*w];

                r1 = (newcolor >> 16) & 0xFF;
                g1 = (newcolor >> 8) & 0xFF;
                b1 = (newcolor >> 0) & 0xFF;

                int newcolor2 = oldPx[x+(y+1)*w];

                r2 = (newcolor2 >> 16) & 0xFF;
                g2 = (newcolor2 >> 8) & 0xFF;
                b2 = (newcolor2 >> 0) & 0xFF;

                int tr = (int) (2*Math.sqrt(((r-r1)*(r-r1)+(r-r2)*(r-r2))));
                int tg = (int) (2*Math.sqrt(((g-g1)*(g-g1)+(g-g2)*(g-g2))));
                int tb = (int) (2*Math.sqrt(((b-b1)*(b-b1)+(b-b2)*(b-b2))));

                newPx[x+y*w] = (255 << 24)|(tr << 16)|(tg << 8)|(tb);
            }
        }
        resultBitmap.setPixels(newPx, 0, w, 0, 0, w, h);
        return resultBitmap;
    }
}
